﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class suggestion_bll : base_dll
    {
        public PaginationData<Wjtour.Domain.suggestion> GetAuditList(string q, int page, int pagesize = 20, int isaudit = 2)
        {
            string where = " where 1=1 and isaudit=" + isaudit;
            if (!string.IsNullOrEmpty(q))
                where += " and title like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from suggestion 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.suggestion> data = GetPaginationData<Wjtour.Domain.suggestion>(sql, page, pagesize);
            return data;
        }

        public PaginationData<Wjtour.Domain.suggestion> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and title like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from suggestion 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.suggestion> data = GetPaginationData<Wjtour.Domain.suggestion>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.suggestion Load(int id)
        {
            string sql = "select * from suggestion where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.suggestion>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.suggestion entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE suggestion SET 
            name = @name,
            phone = @phone,
            email = @email,
            title = @title,
            content = @content,
            addtime = @addtime,
            replayer = @replayer,
            replaycontent = @replaycontent,
            replaytime = @replaytime,
            isaudit = @isaudit
             WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO suggestion( 
                name,phone,email,title,content,addtime,replayer,replaycontent,replaytime,isaudit)VALUES(
                @name,@phone,@email,@title,@content,@addtime,@replayer,@replaycontent,@replaytime,@isaudit)";
            }
            DbAccess.AddInParameter(cmd, "@name", DbType.String, entity.name);
            DbAccess.AddInParameter(cmd, "@phone", DbType.String, entity.phone);
            DbAccess.AddInParameter(cmd, "@email", DbType.String, entity.email);
            DbAccess.AddInParameter(cmd, "@title", DbType.String, entity.title);
            DbAccess.AddInParameter(cmd, "@content", DbType.String, entity.content);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@replayer", DbType.String, entity.replayer);
            DbAccess.AddInParameter(cmd, "@replaycontent", DbType.String, entity.replaycontent);
            DbAccess.AddInParameter(cmd, "@replaytime", DbType.DateTime, entity.replaytime);
            DbAccess.AddInParameter(cmd, "@isaudit", DbType.Int32, entity.isaudit);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from suggestion where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }

        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="audit"></param>
        /// <returns></returns>
        public int BatchAudit(string ids, int audit)
        {
            string sql = "update suggestion set isaudit = " + audit + " where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
